package com.cncn.youmengsharelib.ui;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cncn.youmengsharelib.SharePlatform;
import com.cncn.youmengsharelib.R;

import java.util.List;

/**
 * Created by Vivi__Peng on 2015/11/3.
 */
public class PlateformsAdapter extends BaseAdapter {
    private List<SharePlatform> platformList;
    private Context mContext;
    private LayoutInflater mInflater;

    private float mDrawableSizeWidth;
    private float mDrawableSizeHeight;

    public PlateformsAdapter(Context context, List<SharePlatform> list) {
        mContext = context;
        platformList = list;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mDrawableSizeWidth = dip2px(context, 40);
        mDrawableSizeHeight = dip2px(context, 40);
    }

    @Override
    public int getCount() {
        return platformList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final SharePlatform sharePlat = platformList.get(position);
        View view = mInflater.inflate(R.layout.grid_item_share_platform, null);
        TextView tv = (TextView) view.findViewById(R.id.tvPlatform);
        tv.setText(sharePlat.getName());

        Drawable topDrawable = getDrawable(sharePlat.getDrawableId(), mDrawableSizeWidth, mDrawableSizeHeight);
        tv.setCompoundDrawables(null, topDrawable, null, null);

        /*tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick(sportsType.getType());
            }
        });*/

        view.setTag(sharePlat);

        return view;

    }

    private Drawable getDrawable(int drawableId, float width, float height) {
        Drawable drawable = null;
        if (drawableId > 0) {
            drawable = getDrawable(mContext.getResources().getDrawable(drawableId), width, height);
        }
        return drawable;
    }

    private Drawable getDrawable(Drawable drawable, float width, float height) {
        if (drawable != null) {
            float drawableWidth = width >= 0 ? width : drawable.getMinimumWidth();
            float drawableHeight = height >= 0 ? height : drawable.getMinimumHeight();
            drawable.setBounds(0, 0, (int) drawableWidth, (int) drawableHeight);
        }
        return drawable;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    private static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }



}
